/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package expression.interval;

/**
 *
 * @author ddoose
 */
public abstract class Interval {
    
    public static Interval createEmpty() {
        return new IntEmpty();
    }
    
    public static Interval createUniverse() {
        return new IntUniverse();
    }
    
    public static Interval createLeftBounded(int b) {
        return new IntLeftBounded(b);
    }
    
    public static Interval createRightBounded(int b) {
        return new IntRightBounded(b);
    }
    
    public static Interval createValue(int v) {
        return new IntValue(v);
    }
    
    public static Interval createBounded(int l, int r) {
        if (l == r) {
            return new IntValue(l);
        }
        else if (l > r) {
            return new IntEmpty();
        }
        else {
            return new IntBounded(l, r);
        }
    }
    
    public abstract boolean isEmpty();
    
    public abstract boolean isUniverse();
    
    public abstract boolean isLeftBounded();
    
    public abstract boolean isRightBounded();
    
    public abstract boolean isValue();
    
    public abstract int getLeftBound();
    
    public abstract int getRightBound();
    
    public abstract int getValue();
    
    public abstract Interval inter(Interval i);
    
    /**
     * 
     * @param i
     * @return null si les deux intervalles sont disjoints
     */
    public abstract Interval union(Interval i);
    
    /**
     * Les deux intervalles sont disjoints et toutes les valeurs de self sont strictement inférieures à celles de i.
     * @param i
     * @return
     */
    public boolean isStrictLeft(Interval i) {
        if (isRightBounded() && i.isLeftBounded() && (getRightBound() < i.getLeftBound())) {
            return true;
        }
        return false;
    }
    
    /**
     * Les deux intervalles sont disjoints et toutes les valeurs de self sont strictement supérieures à celles de i.
     * @param i
     * @return
     */
    public boolean isStrictRight(Interval i) {
        if (isLeftBounded() && i.isRightBounded() && (getLeftBound() > i.getRightBound())) {
            return true;
        }
        return false;
    }
    
    public boolean isDisjoined(Interval i) {
        return isStrictLeft(i) || isStrictRight(i);
    }
    
    /**
     * Self possède des valeurs qui sont strictement inférieures à i.
     * @param i
     * @return
     */
    public boolean isLeft(Interval i) {
        if (!isLeftBounded()) {
            if (i.isLeftBounded()) {
                return true;
            }
        }
        else {
            if (i.isLeftBounded() && (getLeftBound() < i.getLeftBound())) {
                return true;
            }
        }
        
        return false;
    }
    
    /**
     * Self possède des valeurs qui sont strictement supérieures à i.
     * @param i
     * @return
     */
    public boolean  isRight(Interval i) {
        if (!isRightBounded()) {
            if (i.isRightBounded()) {
                return true;
            }
        }
        else {
            if (i.isRightBounded() && (getRightBound() > i.getRightBound())) {
                return true;
            }
        }
        
        return false;
    }
    
    public abstract boolean equals(Interval i);
    
    public abstract Interval clone();
    
    public abstract String toString();
    
    public static Interval inter(Interval[] i) {
        Interval inter = new IntUniverse();
        for (Interval j: i) {
            inter = inter.inter(j);
        }
        return inter;
    }
    
    
    public static void main(String[] args) {
        Interval i1 = new IntEmpty();
        Interval i2 = new IntUniverse();
        Interval i3 = new IntValue(10);
        Interval i4 = new IntLeftBounded(20);
        Interval i5 = new IntRightBounded(30);
        Interval i6 = new IntBounded(10, 20);
        
        System.out.println(i1);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println(i4);
        System.out.println(i5);
        System.out.println(i6);
    }
}
